/*
 * Copyright (c) 2012 Keerthi Ramachandran .
 */
package com.kee.example.dao;

import java.util.Set;

import com.kee.example.domain.Event;
import com.kee.example.domain.Person;
import com.kee.example.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 * This class... todo (RK) javadoc
 *
 * @author <a href="mailto:Keerthi.Ramachandran@navis.com">Keerthi Ramachandran</a>, 8/23/12
 * @since 2.4. /2012-08-23
 */
public class PersonDao extends BaseDaoImpl {
    private Session _session;
    @Override
    public void save(Object inObject) {
        Person person = (Person) inObject;
        Set<Event> set = person.getEvents();
        for(Event event:set){
            if((event.getId()) == 0){
                EventDao eventDao = new EventDao();
                eventDao.save(event);
            }
        }
        super.save(inObject);
    }

    public Person getPerson(String inFirstName, String inLastName){
      _session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = _session.createCriteria(Person.class)
                .add(Restrictions.eq("firstName",inFirstName))
                .add(Restrictions.eq("lastName",inLastName));
        return (Person) criteria.uniqueResult();
    }

}
